package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildManagerV2;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.formation.Formation;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class AssistListResponse extends Response {

	private List<AssistFriendInfo> list;
	private int assisted;
	private int maxassist;
	
	public class AssistFriendInfo{
		public long id;
		public String name;
		public String img;
		public int sex;
		public int lv;
		public int assState;//0未协助  1协防中  2今日已协防过
		public AssistFriendInfo(Player player, int state){
			this.id = player.getId();
			this.name = player.getName();
			this.img = player.getIcon();
			this.sex = player.getSex();
			this.lv = player.getLevel();
			this.assState = state;
		}
	}
	
	public AssistListResponse(int serial, Player player, List<Player> friends) {
		super(serial);
		list = new ArrayList<AssistFriendInfo>();
//		int count = 0;
//		List<Employee> eList = null;
//		int eCount = 4;
		for(int i = 0, size = friends.size(); i < size; i++){
			Player frd = friends.get(i);
			
//			Formation f = frd.getFormations().get(Formation.TYPE_NORMAL);
//			if (f != null) {
//				eList = Arrays.asList(f.getUnits());
//			}
//			if(eList == null || eList.size() != eCount){
//				continue;
//			}
//			boolean needContinue = false;
//			for(int j = 0; j < eCount; j++){
//				Employee e = eList.get(i);
//				if(e == null){
//					needContinue = true;
//					break;
//				}
//			}
//			if(needContinue){
//				continue;
//			}
			BuildV2 build = player.getBuildings2().getAssistBuildByAssistant(frd.getId());
			if(build != null){
				//协防中的
				list.add(new AssistFriendInfo(frd, 1));
//				count++;
			}else{
				if(player.getBuildings2().checkIsAssist(frd.getId())){
					list.add(new AssistFriendInfo(frd, 2));
//					count++;
				}else{
					list.add(new AssistFriendInfo(frd, 0));
				}
			}
		}
		this.assisted = player.getBuildings2().getAssSize();
		this.maxassist = BuildManagerV2.BUILD_MAX_ASSISTANT_COUNT;
	}

	public List<AssistFriendInfo> getList() {
		return list;
	}

	public void setList(List<AssistFriendInfo> list) {
		this.list = list;
	}

	public int getAssisted() {
		return assisted;
	}

	public void setAssisted(int assisted) {
		this.assisted = assisted;
	}

	public int getMaxassist() {
		return maxassist;
	}

	public void setMaxassist(int maxassist) {
		this.maxassist = maxassist;
	}
}
